import 'package:flutter/material.dart';
import 'package:flutter_hx/theme/font_size.dart';
import 'package:get/get.dart';

class CommonDialog extends StatelessWidget {
  const CommonDialog({
    Key? key,
    this.onConfirm,
    this.onCancel,
    this.content,
    this.title,
    this.text,
    this.hideCancel = false,
    this.backgroundReturn = true,
    this.cancelColor = const Color(0xFF323233),
    this.conformColor = const Color(0xFF00C3B3),
    this.confirmText = "确定",
    this.cancelText = "取消",
  }) : super(key: key);
  final Function? onConfirm;
  final Function? onCancel;
  final String confirmText;
  final String cancelText;
  final bool hideCancel;

  ///点击背景返回
  final bool backgroundReturn;
  final Color cancelColor;
  final Color conformColor;

  ///内容
  final String? title;
  final String? text;

  ///当内容widget不为空，则将标题和文本替换成该widget
  final Widget? content;

  @override
  Widget build(BuildContext context) {
    MediaQuery.of(context).size;
    return GestureDetector(
      onTap: backgroundReturn ? () => Get.back() : null,
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: Center(
          child: GestureDetector(
            onTap: () {},
            child: Container(
              width: 311,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16),
                color: Colors.white,
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  ///上部内容
                  Padding(
                    padding: const EdgeInsets.all(24),
                    child: content ??
                        Column(
                          children: [
                            Text(
                              title ?? "",
                              style: const TextStyle(
                                  color: Color(0xFF323233),
                                  fontSize: FontSize.pt16),
                            ),
                            const SizedBox(
                              height: 8,
                            ),
                            Text(
                              text ?? "",
                              style: const TextStyle(
                                  color: Color(0xFF969799),
                                  fontSize: FontSize.pt14),
                            )
                          ],
                        ),
                  ),
                  Container(
                    color: const Color(0xFFEBEDF0),
                    height: 0.5,
                  ),
                  SizedBox(
                    ///按钮高度
                    height: 48,
                    child: LayoutBuilder(builder: (context, constraint) {
                      return Row(
                        children: [
                          if (!hideCancel)
                            _interactButton(
                                color: cancelColor,
                                constraint: constraint,
                                text: cancelText,
                                onTap: () {
                                  onCancel?.call();
                                  Get.back();
                                }),
                          if (!hideCancel)
                            Container(
                              color: const Color(0xFFEBEDF0),
                              width: 0.5,
                            ),
                          _interactButton(
                              constraint: constraint,
                              text: confirmText,
                              color: conformColor,
                              onTap: () {
                                onConfirm?.call();
                                Get.back();
                              })
                        ],
                      );
                    }),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _interactButton(
      {required BoxConstraints constraint,
      Function? onTap,
      required String text,
      required Color color}) {
    return Expanded(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => onTap?.call(),
        child: SizedBox(
          height: constraint.maxHeight,
          child: Center(
            child: Text(
              text,
              style: TextStyle(color: color, fontSize: FontSize.pt16),
            ),
          ),
        ),
      ),
    );
  }
}
